﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinQ_DTO;
namespace DAO
{
    public class Sanpham_khuyenmaiDAO
    {
        public List<shop_sanpham_khuyenmai> LayTatCaCacSanPham_KhuyenMai()
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var SP_KM = from sp_km in EFashionDB.shop_sanpham_khuyenmais select sp_km;
            return SP_KM.ToList();

        }

        public List<shop_sanpham> LayTatCaSanPhamTheoMakhuyenMai(int MaKM)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var SP_KM = from sp_km in EFashionDB.shop_sanpham_khuyenmais
                        from sp in EFashionDB.shop_sanphams
                        where sp.MaSanPham == sp_km.MaSanPham && sp_km.MaKhuyenMai == MaKM
                        select sp;
            return SP_KM.ToList();

        }

        public List<shop_khuyenmai> LayKhuyenMaiTheoMaSanPham(int MaSP)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var KhuyenMai = from sp_km in EFashionDB.shop_sanpham_khuyenmais
                            from km in EFashionDB.shop_khuyenmais
                            where sp_km.MaKhuyenMai == km.MaKhuyenMai && sp_km.MaSanPham == MaSP
                            select km;
            return KhuyenMai.ToList();
 
        }

        public void ThemSanPham_KhuyenMai (shop_sanpham_khuyenmai sp_km)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            EFashionDB.shop_sanpham_khuyenmais.InsertOnSubmit(sp_km);
            EFashionDB.SubmitChanges();
        }

        public void XoaSanPham_KhuyenMaiTheoMaSP(int MaSP)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var ListSp_km = from sp_km in EFashionDB.shop_sanpham_khuyenmais
                            where sp_km.MaSanPham == MaSP
                            select sp_km;
            EFashionDB.shop_sanpham_khuyenmais.DeleteAllOnSubmit(ListSp_km);
            EFashionDB.SubmitChanges();

                                    
        }
        public void XoaSanPham_KhuyenMaiTheoMaKM(int MaKM)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var ListSp_km = from sp_km in EFashionDB.shop_sanpham_khuyenmais
                            where sp_km.MaKhuyenMai == MaKM
                            select sp_km;
            EFashionDB.shop_sanpham_khuyenmais.DeleteAllOnSubmit(ListSp_km);
            EFashionDB.SubmitChanges();

        }
        public void CapNhatSanPham_KhuyenMai(int MaSP, int MaKMCu, int MaKMMoi)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var Sp_Km = from sp_km in EFashionDB.shop_sanpham_khuyenmais
                        where sp_km.MaSanPham == MaSP  && sp_km.MaKhuyenMai == MaKMCu
                        select sp_km;
            shop_sanpham_khuyenmai SanP_KhuyenM = Sp_Km.Single();
            SanP_KhuyenM.MaKhuyenMai = MaKMMoi;
            EFashionDB.SubmitChanges();

        }

    }
}
